<?php


namespace app\admin\controller;


use app\common\controller\Common;
use think\Controller;
use think\Db;
use think\Exception;
use think\Request;
use think\Session;
use think\Validate;

class Affiche extends Controller
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        //获取平台标题
        $platform = Db::name("config_platform")->find();
        $this->assign("platform", $platform);
        $swidthStyle = Session::get("style");
        $this->assign("swidthStyle",$swidthStyle);
    }

    public function index()
    {
        //权限查询
        $common = new Common();
        $powerList = $common->showpower();
        $this->assign("powerList",$powerList);
        $keyword = $status = $community_id = '';
        //获取内容列表
        $where = [];
        $community_list[0] = ['id' => -1, 'propertyName' => "平台公告"];
        $this->assign("community_list", $community_list);

        if (!empty($_GET['keyword'])) {
            $where['title'] = ['like', "%" . $_GET['keyword'] . "%"];
            $keyword = $_GET['keyword'];
        }
        if (!empty($_GET['status'])) {
            $where['status'] = $_GET['status'] == -1 ? 0 : $_GET['status'];
            $status = $_GET['status'];
        }

        $thisWechat = Session::get("thisWechat");
        $where['uniacid'] = $thisWechat['id'];
        if (!empty($_GET['type'])) {
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                //获取列表
                $list = Db::name("admin_affiche")->order("sort desc,id desc")->where($where)->limit($start,$limit)->select();
                //字段拼接
                foreach ($list as $k => $v) {
                    $list[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
                }
                $count = Db::name("admin_affiche")->order("id desc")->where($where)->count();
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }


        $assign = [
            "keyword" => $keyword,
            "status" => $status,
            "community_id" => intval($community_id)
        ];
        $this->assign($assign);
        return $this->fetch('index');
    }

    //添加视图
    public function addAffiche()
    {
        $community[0] = ['id' => 0, 'propertyName' => "平台公告"];
        $this->assign("community", $community);
        //启用状态
        $enable_status = Db::name("config_attribute")->where(['key' => "enable_status"])->value("value");
        $enable_status = explode("|", $enable_status);
        foreach ($enable_status as $k => $v) {
            $enable_status[$k + 1] = $v;
        }
        unset($enable_status[0]);
        $this->assign("enable_status", $enable_status);


        //获取基本信息
        $id = empty($_GET['id'])?0:$_GET['id'];
        $detail = Db::name("admin_affiche")->where(['id' => $id])->find();
        if(empty($detail)){
            $showTable = "us_admin_affiche";
            $queryTable = Db::query("show columns from ".$showTable); //获取表结构
            foreach ($queryTable as $item) {
                $detail[$item['Field']] = "";
            };
        }

        $this->assign("detail", $detail);
        return $this->fetch('update');
    }

    //添加公告操作
    public function doAffiche()
    {
        try {
            $common = new Common();
            $rule = [
                ["title", "require", "请填写标题"],
//                ["content", "require", "请填写内容"],
//                    ["remark", "require", "请填写备注"],
            ];
            $validate = new Validate($rule);
            $data = $this->request->post();
            $thisWechat = Session::get("thisWechat");
            $data['uniacid'] = $thisWechat['id'];
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }
            //查询小区名称
            $propertyName = "平台";
            if (empty($data['id'])) {
                $data['addtime'] = time();
                $data['add_id'] = Session::get("id");
                $data['auto'] = Session::get("auto");
                $id = Db::name("admin_affiche")->insertGetId($data);
                $log['remark'] = "【" . Session::get("user_name") . "】发布新的公告【" . $data['title'] . "】,id【" . $id . "】";
            } else {
                $data['updatetime'] = time();
                $data['update_id'] = Session::get("id");
                $data['add_id'] = Session::get("id");
                $data['auto'] = Session::get("auto");
                $id = $data['id'];
                Db::name("admin_affiche")->update($data);
                $log['remark'] = "【" . Session::get("user_name") . "】更新公告【" . $data['title'] . "】,id【" . $id . "】";
            }
            $log['controller'] = "Affiche";
            $log['methods'] = "doAffiche";
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "操作成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


    //删除公告
    public function delAffiche()
    {
        try {
            $id = $_POST['id'];
            $list = Db::name("admin_affiche")->where(['id' => ['in', $id]])->select();
            $res = Db::name("admin_affiche")->where(['id' => ['in', $id]])->delete();
            if (empty($res)) {
                throw new Exception("删除失败");
            }
            $common = new Common();
            foreach ($list as $k => $v) {
                $log['remark'] = "【" . Session::get("user_name") . "】删除公告【" . $v['title'] . "】";
                $log['controller'] = "Affiche";
                $log['methods'] = "delAffiche";
            }
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

}